<template>
  <div class="tab-container">
    <!-- <el-tag>mounted times ：{{ createdTimes }}</el-tag> -->
    <!-- <el-alert :closable="false" style="width:200px;display:inline-block;vertical-align: middle;margin-left:30px;" title="Tab with keep-alive" type="success" /> -->
    <el-tabs v-model="activeName" style="margin-top:15px;" type="border-card" @tab-click="handleTabClick">
      <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key">
        <!-- <keep-alive> -->
        <tab-pane
          v-if="activeName==item.key"
          :type="item.key"
          :table-label="tableLabel"
          :table-data="tableData"
          :list-query="listQuery"
          @getHandleProductEditor="handleProductEditorWay"
          @handleTabChangeStatus="handleChangeStatus"
          @handleTabDel="handleProductDel"
          @create="showCreatedTimes"
          @updateThing="handleEditor"
          @getListThing="handleRefreshList"
        />
        <!-- </keep-alive> -->
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import TabPane from './components/TabPane'

export default {
  name: 'Tab',
  components: { TabPane },
  props: {
    tabMapOptions: {
      type: Array,
      default: Array
    },
    tableLabel: {
      type: Array,
      default: function() {
        return []
      }
    },
    tableData: {
      type: Array,
      default: function() {
        return []
      }
    },
    listQuery: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      // tabMapOptions: [
      //   { label: 'China', key: 'CN' },
      //   { label: 'USA', key: 'US' },
      //   { label: 'Japan', key: 'JP' },
      //   { label: 'Eurozone', key: 'EU' }
      // ],
      activeName: 'All',
      createdTimes: 0
    }
  },
  watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    }
  },
  created() {
    // init the default selected tab
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
  methods: {
    handleTabClick(tab) {
      console.log(tab.name)
      this.$emit('handleTabChange', tab.name)
    },
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
    },
    // 修改商品
    handleProductEditorWay(data) {
      this.$emit('getHandleProductEditor', data)
    },
    // 商品上下架
    handleChangeStatus(data) {
      this.$emit('handleListChangeStatus', data)
    },
    // 商品删除
    handleProductDel(data) {
      this.$emit('handleListDel', data)
    },
    // 编辑
    handleEditor(data) {
      this.$emit('dataEditor', data)
    },
    // 刷新列表
    handleRefreshList() {
      this.$emit('RefreshListWay')
    }
  }
}
</script>

<style scoped>
  /* .tab-container {
    margin: 30px;
  } */
</style>
